package view;

import com.hexidec.util.Translatrix;

import model.LessonGuide;
import eventHandling.Callback;
import eventHandling.EventBus;
import eventHandling.EventTyp;
import eventHandling.EventTyp.Type;

/**
 * The CreateCardDialogController is the Controller for CreateCardDialog.
 * 
 * 
 * @author sloth42
 *
 */
public class CreateCardDialogController implements Controller {

	private CreateCardDialog createCardDialog;
	private EventBus eventBus;
	private LessonGuide lessonGuide;

	public CreateCardDialogController(EventBus eventBus,
			CreateCardDialog createCardDialog, LessonGuide lessonGuide) {
		this.eventBus = eventBus;
		this.createCardDialog = createCardDialog;
		this.lessonGuide = lessonGuide;
		registerForEvents();
	}

	private void registerForEvents() {
		
		eventBus.register(Type.OPEN_ADD_CARD_DIALOG, new Callback() {
			
			public void actionPerformed() {
				createCardDialog.setTitle(Translatrix.getTranslationString("createNewCard"));
				createCardDialog.setVisible(true);
			}
		});
		
		eventBus.register(EventTyp.Type.CLOSE_CREATE_CARD_DIALOG,
				new Callback() {
			
					public void actionPerformed() {
						createCardDialog.setVisible(false);
					}
				});

		eventBus.register(EventTyp.Type.ADD_NEW_CARD, new Callback() {
			
			public void actionPerformed() {

				lessonGuide.createNewCard(createCardDialog.getEditCardPanel()
						.getFrontSide(), createCardDialog.getEditCardPanel()
						.getReverseSide());

				//TODO delete this if not needed any more for testing
				//Wie sieht der HTML Quellcode aus?
//					JDialog temp = new JDialog();
//					JTextArea ta = new JTextArea();
//					ta.setText(createCardDialog.getEditCardPanel().getFrontSideText());
//					temp.add(new JPanel().add(ta));
//					temp.setSize(150, 150);
//					temp.setVisible(true);

				if (!createCardDialog.keepDialogOpen()) {
					createCardDialog.setVisible(false);
				}
			}
		});

	}

}
